package com.hnist.empdemo.view.component;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.VBox;

public class GenderPieChart {
    private VBox container;
    private PieChart pieChart;

    public GenderPieChart(int maleCount, int femaleCount) {
        container = new VBox(10);
        container.setAlignment(javafx.geometry.Pos.CENTER);
        initPieChart(maleCount, femaleCount);
        container.getChildren().add(pieChart);
    }

    private void initPieChart(int maleCount, int femaleCount) {
        // 计算总人数和百分比（处理除零问题）
        int total = maleCount + femaleCount;
        String maleLabel = String.format("男 (%.1f%%)", total > 0 ? (maleCount * 100.0 / total) : 0);
        String femaleLabel = String.format("女 (%.1f%%)", total > 0 ? (femaleCount * 100.0 / total) : 0);

        // 创建饼图数据
        ObservableList<PieChart.Data> pieData = FXCollections.observableArrayList(
                new PieChart.Data(maleLabel, maleCount),
                new PieChart.Data(femaleLabel, femaleCount)
        );

        // 初始化饼图
        pieChart = new PieChart(pieData);
        pieChart.setPrefSize(300, 300);
        pieChart.setAnimated(true);
        pieChart.setLegendVisible(true);
        pieChart.setLabelsVisible(true); // 显示标签

        // 设置样式
        pieChart.setStyle("-fx-font-size: 14px;");
        pieData.forEach(data -> {
            data.getNode().setStyle("-fx-font-size: 14px;");
        });
    }

    public VBox getVBox() {
        return container;
    }
}